/**
 * @date 2006-7-18
 */

package org.nanhill.xmlro.gsp;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nanhill.xmlro.gsp.client.DefaultGspAgent;
import org.nanhill.xmlro.gsp.client.GspAgent;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-7-18
 */
public class SearchSample {

	private static long getRetcode(Map result) {
		Map map = (Map) result;
		return (Long) map.get("retcode");
	}

	private static void doSearch(GspAgent searchAgent) throws GspException,
			IOException {
		// ------------- search --------------------------
		Map<String, Object> search = new HashMap<String, Object>();
		search.put("words", "杭州");
		search.put("class#", 2L);
		search.put("limit", 1000L);
		search.put("rec_page", 15L);
		// search.put("sessionID", null);
		Map searchRs = (Map) searchAgent.doService("search", search);
		System.out.println("search return code:"
				+ SearchSample.getRetcode(searchRs));
		List classes = (List) searchRs.get("classes");
		for (Object eachClss : classes) {
			Map eachMap = (Map) eachClss;
			System.out.println(eachMap.get("alias"));
		}
		List messages = (List) searchRs.get("messages");
		if (messages.size() != 0) {
			Map message = (Map) messages.get(0);
			long messageId = (Long) message.get("message#");
			doDetail(searchAgent, messageId);
		}

	}

	private static void doDetail(GspAgent searchAgent, long messageId)
			throws GspException, IOException {
		// ------------- search --------------------------
		Map<String, Object> search = new HashMap<String, Object>();
		search.put("message#", messageId);
		search.put("server", "dis1");
		Map searchRs = (Map) searchAgent.doService("fetchMessage", search);

		System.out.println("search return code:"
				+ SearchSample.getRetcode(searchRs));
		System.out.println(searchRs.get("message"));
		// List classes = (List) searchRs.get("classes");
		// for (Object eachClss : classes) {
		// Map eachMap = (Map) eachClss;
		// System.out.println(eachMap.get("class"));
		// }

	}

	/**
	 * @param args
	 * @throws GspException
	 */
	public static void main(String[] args) {
		GspAgent searchAgent = null;
		try {
			// ===== sls location =====
			searchAgent = new DefaultGspAgent("hawaii.kernel.sls");
			searchAgent.addSLSTarget("222.68.194.5", 2001);
			searchAgent.setServiceType("centaurus.uiservice", "1");
			searchAgent.locate();
			SearchSample.doSearch(searchAgent);
			searchAgent.close();
			// ========= direct service location , not sls ========
			searchAgent = new DefaultGspAgent("hawaii.kernel.sls");
			searchAgent.setServiceHost("192.168.3.9");
			searchAgent.setServicePort(10004);
			searchAgent.setServiceType("centaurus.uiservice", "1");
			//SearchSample.doSearch(searchAgent);
			searchAgent.close();
		} catch (GspException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
